package com.example.w.controller;

import com.example.w.dto.PunchRecord;
import com.example.w.service.PunchCardService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/v1/punch")
public class PunchCardController {

    @Autowired
    private PunchCardService punchCardService;

    //获取企业授权码
    @PostMapping("/getAuthCode")
    public String getAuthCode(@RequestParam("companyCode") String companyCode) {
        log.info("获取企业授权码：{}", companyCode);
        String authcode = punchCardService.getAuthCode(companyCode);
        if (authcode != null) {
            return "{\"code\":0,\"message\":\"OK\",\"企业授权码为\":\"" + authcode + "\"}";
        } else {
            return "{\"code\":1,\"message\":\"打卡机厂商编码无效\"}";
        }
    }

    //处理打卡机打卡数据
    @PostMapping("/record")
    public ResponseEntity<String> receivePunchRecord(@RequestBody PunchRecord record) {
        log.info("处理打卡机打卡数据：{}", record);

        try {
            punchCardService.processPunchRecord(record);
            return new ResponseEntity<>("打卡数据处理成功", HttpStatus.OK);
        } catch (Exception e) {
            return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }
}
